package com.sgb.kjwl.viewmodel.home;

import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProvinceEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.home.ChooseAreaActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.PinyinUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChooseAreaModel extends BaseViewModel {
    private HomeRepertory repertory;
    private ChooseAreaActivity activity;

    public ChooseAreaModel(ChooseAreaActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public String getAreaNameModel() {
        return repertory.getAreaName();
    }

    public String getAreaCodeModel() {
        return repertory.getAreaCode();
    }

    public void getAllArealModel() {
        //获取缓存哈希值
        String mHash = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.CacheProvinceData);
        Type type = new TypeToken<List<ProvinceEntity<List<ProvinceEntity>>>>() {
        }.getType();
        Object obj = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.CacheProvinceData, type);
        if (null != obj) {
            List<ProvinceEntity<List<ProvinceEntity>>> provinceList = (List<ProvinceEntity<List<ProvinceEntity>>>) obj;
            activity.setDatas(provinceList);
        } else {
            APIManager.getInstance().getCommonAPI().getCurrentArea().enqueue(new Callback<BaseEntity<List<ProvinceEntity<List<ProvinceEntity>>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<List<ProvinceEntity<List<ProvinceEntity>>>>> call, Response<BaseEntity<List<ProvinceEntity<List<ProvinceEntity>>>>> response) {
                    if (response.body() == null)
                        return;
                    if (response.body().getData() == null)
                        return;
                    List<ProvinceEntity<List<ProvinceEntity>>> provinceList = new ArrayList<>();
                    String mHashNet = DatalUtils.getInstance().strToHashKey(response.body().getData());
                    if (!mHashNet.equals(mHash)) {
                        provinceList.addAll(response.body().getData());
                        activity.setDatas(provinceList);
                        DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.CacheProvinceData, provinceList);
                    }
                    activity.setDatas(response.body().getData());
                }

                @Override
                public void onFailure(Call<BaseEntity<List<ProvinceEntity<List<ProvinceEntity>>>>> call, Throwable t) {

                }
            });
        }
    }

    public void searchFilterModel(List<ProvinceEntity<List<ProvinceEntity>>> entityList, String condition) {
        if (entityList == null) {
            return;
        }
        List<ProvinceEntity<List<ProvinceEntity>>> filterList = new ArrayList<>();
        if (condition.equals("")) {
            activity.showAllArea(entityList);
            return;
        }
        for (ProvinceEntity<List<ProvinceEntity>> entity : entityList) {
            String name = entity.getAreaName();
            String pinyin = PinyinUtil.chineneToSpell(name);
            if (pinyin.contains(condition) || name.contains(condition)) {
                filterList.add(entity);
            }
        }
        activity.showAllArea(filterList);
    }

    public int getSelectModel(String currentId, List<ProvinceEntity<List<ProvinceEntity>>> datas) {
        for (int i = 0; i < datas.size(); i++) {
            if (String.valueOf(datas.get(i).getId()).equals(currentId)) {
                return i;
            }
        }
        return 0;
    }

    public List<ProvinceEntity<List<ProvinceEntity>>> getCommonlyUsedArea() {
        List<ProvinceEntity<List<ProvinceEntity>>> dataList;
        Type type = new TypeToken<List<ProvinceEntity<List<ProvinceEntity>>>>() {
        }.getType();
        Object obj = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.COMMONLY_USED_AREA, type);
        if (obj != null) {
            dataList = (List<ProvinceEntity<List<ProvinceEntity>>>) obj;
        } else {
            dataList = new ArrayList<>();
        }
        return dataList;
    }

    public void setCommonlyUsedArea(ProvinceEntity<List<ProvinceEntity>> usedArea) {
        List<ProvinceEntity<List<ProvinceEntity>>> newDataList = new ArrayList<>();
        newDataList.add(usedArea);
        Type type = new TypeToken<List<ProvinceEntity<List<ProvinceEntity>>>>() {
        }.getType();
        Object obj = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.COMMONLY_USED_AREA, type);
        if (obj != null) {
            List<ProvinceEntity<List<ProvinceEntity>>> dataList = (List<ProvinceEntity<List<ProvinceEntity>>>) obj;
            for (ProvinceEntity<List<ProvinceEntity>> provinceEntity : dataList) {
                if (!provinceEntity.getAreaName().equals(usedArea.getAreaName())) {
                    newDataList.add(provinceEntity);
                }
            }
            if (newDataList.size() > 3) {
                newDataList.remove(3);
            }
        }
        DatalUtils.getInstance().putData(MkvUtils.CacheModular, CacheMarker.COMMONLY_USED_AREA, newDataList);
    }
}
